<?php

namespace app\controller\admin\ah;

use app\model\api\ah\AhCareer;
use app\model\api\ah\AhEquipment;
use app\model\api\ah\AhEquipmentPosition;
use laytp\controller\Backend;
use laytp\library\CommonFun;
use think\facade\Db;

/**
 * Class AuxiliaryEquipment
 * @package app\controller\admin\ah
 */
class AuxiliaryEquipment extends Backend
{
    protected $model;

    public function initialize()
    {
        $this->model = new AhEquipment();
        parent::initialize(); // TODO: Change the autogenerated stub
    }
    public function index()
    {
        $position_model = new AhEquipmentPosition();
        $post = input("param.");
        $where   = $this->buildSearchParams();
        $order   = $this->buildOrder();
        $main_ids = $position_model->where('type','auxiliary')->column('id');
        $allData = $this->request->param('all_data');
        if ($allData) {
            $data = $this->model;
            if(isset($post['search_param']['coordinate_id'])&&$post['search_param']['coordinate_id']['condition']=='SPECIAL'){
                $data = $data->whereNull('coordinate_id');
            }
            if(isset($post['search_param']['coordinate_id'])&&$post['search_param']['coordinate_id']['condition']=='SPECIAL2'){
                $data = $data->where(function($query)use($post){
                     return $query->whereNull('coordinate_id')->whereOr('coordinate_id','=',$post['search_param']['coordinate_id']['value']);
                });
            }
            $data = $data->with('coordinate')->where($where)->whereIn('kind',$main_ids)->order($order)->select()->toArray();
            $data = array_map(function ($item){
                if(isset($item['coordinate']['name'])){
                    $item['coordinate_name']= $item['coordinate']['name'];
                }else{
                    $item['coordinate_name'] = '';
                }
                if(isset($item['coordinate']['attr'])){
                    $item['coordinate_attr'] =json_decode($item['coordinate']['attr'],true);
                }else{
                    $item['coordinate_attr']  = '';
                }
                unset($item['coordinate']);
                return $item;
            },$data);
        } else {
            $limit = $this->request->param('limit', 10);
            $data = $this->model;
            $data =$data->with('coordinate')->where($where)->whereIn('kind',$main_ids)->order($order)->paginate($limit)->toArray();
            $data['data'] = array_map(function ($item){
                if(isset($item['coordinate']['name'])){
                    $item['coordinate_name']= $item['coordinate']['name'];
                }else{
                    $item['coordinate_name'] = '';
                }
                if(isset($item['coordinate']['attr'])){
                    $item['coordinate_attr'] =json_decode($item['coordinate']['attr'],true);
                }else{
                    $item['coordinate_attr']  = '';
                }
                unset($item['coordinate']);
                return $item;
            },$data['data']);
        }
        return $this->success('数据获取成功', $data);
    }


    public function add()
    {
        $career = new AhCareer();
        $post     = CommonFun::filterPostData($this->request->post());
        $insData = [
            'name' => $post['name'],
            'role_id' => json_encode($career->column("role_id"),true),
            'icon' => $post['icon'],
            'rarity' => $post['rarity'],
            'kind' => $post['kind'],
            'kind_desc' => (new AhEquipmentPosition())->where('id',$post['kind'])->value("name"),
            'level_limit' => $post['level_limit'],
            'score' => $post['score'],
            'description' => $post['description'],
            'attr_name' => $post['attr_name'],
            'attr_value' => $post['attr_value'],
        ];
        try {
            $save = $this->model->save($insData);
            if (!$save) throw new \Exception('保存失败');
            $this->clearCache();
            return $this->success('保存成功');
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }

    public function edit()
    {
        $post     = CommonFun::filterPostData($this->request->post());
        $detail = $this->model->find($post['id']);
        empty($detail) && $this->error('数据不存在');
        $detail->name = $post['name'];
        $detail->icon = $post['icon'];
        $detail->rarity = $post['rarity'];
        $detail->kind = $post['kind'];
        $detail->kind_desc =  (new AhEquipmentPosition())->where('id',$post['kind'])->value("name");
        $detail->level_limit = $post['level_limit'];
        $detail->score = $post['score'];
        $detail->description = $post['description'];
        $detail->attr_name = $post['attr_name'];
        $detail->attr_value = $post['attr_value'];
        try {
            $save = $detail->save();
            if (!$save) throw new \Exception('保存失败');
            $this->clearCache();
            return $this->success('保存成功');
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }
}